//
//  BBUtil.m
//  BBGameLib
//
//  Created by Eric on 12/23/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "BBUtil.h"

#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
/*----------Common--------------*/
BOOL isiPad(){  
    return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
}

BOOL isiP4(){  
    return ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2);
}

CGFloat scaleiP3(){
    return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad|| 
			(([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2)))? 1.0f : 0.5f;
}

void alert(NSString* message, NSString* title){
	UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:title
													 message:message
													delegate:nil
										   cancelButtonTitle:@"OK"
										   otherButtonTitles:nil] autorelease];
	[alert show];
}
void alertNoTitle(NSString* message){
	UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:nil
													 message:message
													delegate:nil
										   cancelButtonTitle:@"OK"
										   otherButtonTitles:nil] autorelease];
	[alert show];
}
#pragma mark Common - Mac
#elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED)
/*----------Common--------------*/
BOOL isiPad(){  
    return YES;
}

BOOL isiP4(){  
    return NO;
}

CGFloat scaleiP3(){
    return 1;
}

void alert(NSString* message, NSString* title){
}
void alertNoTitle(NSString* message){
}
#endif


/*----------COLLECTION--------------*/
@implementation NSSet(BB)
- (BOOL)containsIN:(NSInteger)aKey{
    return [self containsObject:[NSNumber numberWithInteger:aKey]];
}
@end

@implementation NSArray(BB)
- (BOOL)containsIN:(NSInteger)aKey{
    return [self containsObject:[NSNumber numberWithInteger:aKey]];
}
@end

@implementation NSMutableArray(BB)
- (void)addObjectIN:(NSInteger)aInt{
    [self addObject:[NSNumber numberWithInteger:aInt]];
}
- (void)removeObjectIN:(NSInteger)aInt{
    [self removeObject:[NSNumber numberWithInteger:aInt]];
}
@end


@implementation NSDictionary(BB)
- (double)doubleValue:(id)key defaultVal:(double)defaultVal{
    id data = [self objectForKey:key];
    if (data) {
        return [data doubleValue];
    }else{
        return defaultVal;
    }
    
}
- (float)floatValue:(id)key defaultVal:(float)defaultVal{
    id data = [self objectForKey:key];
    if (data) {
        return [data floatValue];
    }else{
        return defaultVal;
    }
}

- (float)floatValueIN:(NSInteger)key defaultVal:(float)defaultVal{
    return [self floatValue:[NSNumber numberWithInteger:key] defaultVal:defaultVal];
}
- (float)floatValueIS:(NSInteger)key defaultVal:(float)defaultVal{
    return [self floatValue:[NSString stringWithFormat:@"%d", key] defaultVal:defaultVal];
}

- (NSInteger)intValue:(id)key defaultVal:(NSInteger)defaultVal{
    id data = [self objectForKey:key];
    if (data) {
        return [data intValue];
    }else{
        return defaultVal;
    }
}
- (NSInteger)integerValue:(id)key defaultVal:(NSInteger)defaultVal{
    id data = [self objectForKey:key];
    if (data) {
        return [data integerValue];
    }else{
        return defaultVal;
    }
}
- (long long)longLongValue:(id)key defaultVal:(long long)defaultVal{
    id data = [self objectForKey:key];
    if (data) {
        return [data longLongValue];
    }else{
        return defaultVal;
    }
}
- (BOOL)boolValue:(id)key defaultVal:(BOOL)defaultVal{
    id data = [self objectForKey:key];
    if (data) {
        return [data boolValue];
    }else{
        return defaultVal;
    }
}

-(id)value:(id)key defaultVal:(id)defaultVal{
    id data = [self objectForKey:key];
    if (data) {
        return data;
    }else{
        return defaultVal;
    }
}

- (BOOL)containsKeyIN:(NSInteger)aKey{
    return [self.allKeys containsObject:[NSNumber numberWithInteger:aKey]];
}


-(id)objectForKeyIN:(NSInteger)aKey{
    return [self objectForKey:[NSNumber numberWithInteger:aKey]];
}

-(id)objectForKeyIS:(NSInteger)aKey{
    return [self objectForKey:[NSString stringWithFormat:@"%d", aKey]];
}
@end

@implementation NSMutableDictionary(BB)
-(void)setObject:(id)anObject forKeyIN:(NSInteger)aKey{
    [self setObject:anObject forKey:[NSNumber numberWithInteger:aKey]];
}
-(void)setObject:(id)anObject forKeyIS:(NSInteger)aKey{
    [self setObject:anObject forKey:[NSString stringWithFormat:@"%d", aKey]];
}
-(void)removeObjectForKeyIN:(NSInteger)aKey{
    [self removeObjectForKey:[NSNumber numberWithInteger:aKey]];
}
@end

